import cv2
import os

def process_images(directory):
    for filename in os.listdir(directory):
        if filename.endswith(".png") or filename.endswith(".jpg"):
            image_path = os.path.join(directory, filename)
            testimg = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)  # 以灰度模式读取图像
            img_copy = testimg.copy()

            # 二值化
            img_copy[img_copy <= 100] = 0
            img_copy[img_copy > 100] = 1

            # 保存修改后的图像
            cv2.imwrite(image_path, img_copy)

# 指定目录路径
directory = "data/crack_data/train/masks"

# 调用函数处理目录下的图像
process_images(directory)